/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Threads;

import DAO.ChuDeDAO;
import DAO.WebsiteDAO;
import DTO.ChuDeDTO;
import DTO.WebsiteDTO;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author QuangHau
 */
public class loadTreeWebsite extends Thread {

    JTree treeWebsite;
    JScrollPane pnNews;
    JLabel lbstatus;
    JButton btnLayTin;
    int mawebsite;
    int machude;

    public loadTreeWebsite(Object[] args) {
        treeWebsite = (JTree) args[0];
        pnNews = (JScrollPane) args[1];
        lbstatus = (JLabel) args[2];
        btnLayTin = (JButton) args[3];
        mawebsite = (int) args[4];
        machude = (int) args[5];
        btnLayTin.setEnabled(false);
    }

    @Override
    public void run() {
        writeLbStatus wt = null; 
        if (!Comvar.writeStatus) {
            Comvar.writeStatus = true;
            wt = new writeLbStatus(lbstatus, "Đang tải");
            wt.start();
        }
        if (loadTreeWebsite()) {
            loadTinTuc ltt = new loadTinTuc(new Object[]{pnNews, lbstatus, mawebsite, machude, false});
            ltt.start();
            try {
                ltt.join();
            } catch (InterruptedException ex) {
                Logger.getLogger(loadTreeWebsite.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (wt!= null) {
            wt.stopWrite();
            Comvar.writeStatus = false;
        }
        btnLayTin.setEnabled(true);
    }

    private boolean loadTreeWebsite() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Tất cả");
        ArrayList<WebsiteDTO> arrweb = WebsiteDAO.SelectWebsite();
        if (arrweb != null && arrweb.size() > 0) {
            for (int i = 0; i < arrweb.size(); i++) {
                DefaultMutableTreeNode website = new DefaultMutableTreeNode(arrweb.get(i));
                ArrayList<ChuDeDTO> arrchude = ChuDeDAO.selectByWebsite(arrweb.get(i).getMaWebsite());
                if (arrchude != null && arrchude.size() > 0) {
                    for (int j = 0; j < arrchude.size(); j++) {
                        DefaultMutableTreeNode category = new DefaultMutableTreeNode(arrchude.get(j));
                        website.add(category);
                    }
                }
                root.add(website);
            }
            TreeModel mode = new DefaultTreeModel(root);
            treeWebsite.setModel(mode);
            treeWebsite.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
            return true;
        }
        treeWebsite.setModel(null);
        return false;
    }
}
